package com.bds.portal.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bds.model.Result;
import com.bds.model.Template;
import com.bds.portal.common.result.Page;
import com.bds.portal.common.result.USession;
import com.bds.portal.service.MyService;
import com.bds.portal.service.MyTUrlService;
import com.bds.portal.service.UserService;
import com.bds.portal.util.Const;

@Controller
@RequestMapping("/action/my/url")
public class TUrlAction extends BaseAction {
	private static Logger logger = Logger.getLogger(TUrlAction.class);
	@Resource
	private UserService userService;
	@Resource
	private MyService myService;

	@Resource
	private MyTUrlService myTUrlService;

	@RequestMapping(value = "/toUrl/{requestid}")
	public ModelAndView toCompProject(HttpServletRequest request, HttpServletResponse response,
			@PathVariable String requestid) throws Exception {

		ModelAndView view = new ModelAndView();
		try {
			USession us = this.getUserSession(request);
			Template t = myService.getTemplate(requestid);
			view.addObject("request_id", requestid);
			view.addObject("title", t.getTemplate_name());
			view.setViewName("/web/my/snatch/my_url.jsp?mid=a1&aid=ch11");

		} catch (Exception e) {
			logger.error("", e);
			return null;
		}
		return view;
	}

	@RequestMapping(value = "/getMyUrl")
	@ResponseBody
	public Page getMyUrl(HttpServletRequest request, HttpServletResponse response, String request_id, Integer pageNum,
			Integer pageSize,Integer status) throws Exception {
		Page page = new Page();
		try {
			USession us = this.getUserSession(request);
			page = myTUrlService.getTUrl(us.getId(), request_id, pageNum, pageSize,status);
		} catch (Exception e) {
			page.setCode(Const.FAIL_500);
			logger.error("", e);
		}
		return page;
	}

	@RequestMapping(value = "/startUrl")
	@ResponseBody
	public Result startUrl(HttpServletRequest request, HttpServletResponse response, String request_id,Integer status)
			throws Exception {
		Result ret = new Result();
		try {
			USession us = this.getUserSession(request);
			myTUrlService.startUrl(us.getId(), request_id,status);
			ret.setResult(Const.SUC, "操作成功");
		} catch (Exception e) {
			ret.setCode(Const.FAIL_500);
			logger.error("", e);
		}
		return ret;
	}

	@RequestMapping(value = "/saveMyUrl")
	@ResponseBody
	public Result saveMyUrl(HttpServletRequest request, HttpServletResponse response, String urls, String request_id)
			throws Exception {
		Result ret = new Result();
		try {
			String line = System.getProperty("line.separator");
			logger.info("换行符为:" + line);

			if (StringUtils.isEmpty(urls)) {
				ret.setResult(Const.FAIL, "请输入url");
				return ret;
			}

			String[] array = urls.split(line);
			if (array.length == 0) {
				ret.setResult(Const.FAIL, "请输入url");
				return ret;
			}

			if (array.length == 1) {
				array = urls.split("\n");
				if (array.length == 1) {
					array = urls.split("\r");
				}
			}

			USession us = this.getUserSession(request);
			if (myTUrlService.saveTUrl(us.getId(), array, request_id)) {
				ret.setResult(Const.SUC, "操作成功");
			} else {
				ret.setResult(Const.FAIL, "操作失败");
			}
		} catch (Exception e) {
			ret.setCode(Const.FAIL_500);
			logger.error("", e);
		}
		return ret;
	}

}
